<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $this->systemName;?></title>
<link rel="stylesheet" href="<?php echo $this->baseUrl;?>/files/admin/css/main.css" />
<script language="javascript" src="<?php echo $this->baseUrl;?>/files/admin/js/jquery.js"></script>
<link rel="stylesheet" href="<?php echo $this->baseUrl;?>/files/admin/js/formValidator.jquery/style.css" />
<script language="javascript" src="<?php echo $this->baseUrl;?>/files/admin/js/formValidator.jquery/cn.js"></script>
</head>

<body>
	<div id="main">
		<div id="navigation">当前位置：<a href="<?php echo $this->projectUrl(array('action'=>'index'));?>">用户列表</a> &gt; <span id="focus">编辑用户</span></div>
		<div id="body">
			<div id="edit">
				<form id="editForm" name="editForm" method="post" action="">
					<table class="table">
						<tr>
							<td colspan="2">*为必填项</td>
						</tr>
						<tr>
							<td width="20%">用户名：</td>
							<td><?php echo $this->row['userName'];?></td>
						</tr>
						<tr>
							<td width="20%">密码：</td>
							<td>
								<input name="password" type="text" class="validate[callback[checkPassword,1]] input" id="password" value="" size="30" style="width:300px;" />
								<br/>
								密码长度：5-30个字符，不修改请留空							</td>
						</tr>
						<tr>
							<td width="20%">使用状态：</td>
							<td>
								<label>
									<input name="status" type="radio" id="status1" value="1" <?php echo $this->row['status'] == 1 ? ' checked="checked"' : '';?>/> 正常								</label>
								<label>
									<input type="radio" name="status" id="status2" value="2" <?php echo $this->row['status'] == 2 ? ' checked="checked"' : '';?>/> 关闭								</label>							</td>
						</tr>
						<tr>
							<td width="20%">姓名：</td>
							<td><input name="name" type="text" class="validate[required] input" id="name" value="<?php echo htmlspecialchars($this->row['name']);?>" size="30" style="width:300px;" />
							*</td>
						</tr>
						<tr>
							<td width="20%">所在部门：</td>
							<td><input name="department" type="text" class="input" id="department" value="<?php echo htmlspecialchars($this->row['department']);?>" size="30" style="width:300px;" /></td>
						</tr>
						<tr>
							<td width="20%">手机：</td>
							<td><input name="mobile" type="text" class="validate[callback[checkMobile,1]] input" id="mobile" value="<?php echo $this->row['mobile'];?>" size="30" style="width:300px;" /></td>
						</tr>
						<tr>
							<td width="20%">E-Mail：</td>
							<td><input name="email" type="text" class="validate[callback[checkEmail,1]] input" id="email" value="<?php echo $this->row['email'];?>" size="30" style="width:300px;" /></td>
						</tr>
						<tr>
							<td width="20%">用户类型：</td>
							<td>
								<label>
									<input name="type" type="radio" id="type1" class="type" value="1" <?php echo $this->row['type'] == 1 ? ' checked="checked"' : '';?>/> YEK							</label>
								<label>
									<input type="radio" name="type" id="type2" class="type" value="2" <?php echo $this->row['type'] == 2 ? ' checked="checked"' : '';?>/> 商家							</label>
							</td>
						</tr>
						
						<tr id="partnerBlock">
							<td width="20%">所属公司：</td>
							<td>
								<select name="partnerCode" id="partnerCode" onchange="loadRole();">
									<option value="">选择</option>
<?php
foreach($this->partnerRows as $partnerRow) {
?>
									<option value="<?php echo $partnerRow['code'];?>" <?php echo $this->row['partnerCode'] == $partnerRow['code'] ? ' selected' : '';?>><?php echo $partnerRow['name'];?>（<?php echo $partnerRow['code'];?>）</option>
<?php
}
?>
								</select>							</td>
						</tr>
						<tr>
							<td width="20%">所属角色：</td>
							<td><span id="roleBlock"></span></td>
						</tr>
						<tr>
							<td>添加人：</td>
							<td><?php echo htmlspecialchars($this->row['addUserName']);?></td>
						</tr>
						<tr>
							<td>添加时间：</td>
							<td><?php echo htmlspecialchars($this->row['addTime']);?></td>
						</tr>
						<tr>
							<td width="20%">说明：</td>
							<td><textarea name="notes" cols="30" rows="5" class="input" id="notes" style="width:300px;"><?php echo htmlspecialchars($this->row['notes']);?></textarea></td>
						</tr>
						<tr>
							<td width="20%">&nbsp;</td>
							<td><input type="submit" name="button" id="button" value="提交" class="button"/></td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</div>
<script language="javascript">
$(".type").click(function(){setType();});
function setType()
{
	var type = null;
	$(".type").each(function(){
		if ($(this).attr('type') == 'radio' && $(this).attr('checked') == true) {
			type = this.value;
		}
	});
	if (type == 1) {
		$("#partnerBlock").css('display', 'none');
	} else {
		$("#partnerBlock").css('display', '');
	}
	loadRole();
}
$(".type").click(function(){setType();});
$(document).ready(function(){
	setType();
});
</script>
<script language="javascript"> 
function loadRole()
{
	var type = null;
	var userRoleJsonData = <?php echo $this->userRoleJsonData;?>;

	$(".type").each(function(){
		if ($(this).attr('type') == 'radio' && $(this).attr('checked') == true) {
			type = this.value;
		}
	});
    var url = '<?php echo $this->projectUrl(array('action'=>'get-user-role-by-json', 'type'=>'{type}', 'partnerCode'=>'{partnerCode}'), NULL, false);?>'+'?rand/' + Math.random();
	url = url.replace('{type}', type);
	url = url.replace('{partnerCode}', $('#partnerCode').val());
	$("#roleBlock").html('加载中...');
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json",
        async: true,
         success: function(data){
            if (data.length > 0) {
				$("#roleBlock").html('');
				for(var i = 0; i <= data.length - 1; i++) {
					var exists = 0;
					var roleIsAllProduct = null;//似否不限产品，有数据时返回1、0
					var roleHtml = '';
					for (key in userRoleJsonData){
						if (userRoleJsonData[key].roleId == data[i].id) {
							exists = 1;
							roleIsAllProduct = userRoleJsonData[key].isAllProduct;
							break;
						}
					}

					roleHtml += '<span>';
					if (exists == 1) {
						roleHtml += '<div style="margin-top:10px; font-weight:bold;"><label onclick="loadProduct(' + data[i].id + ');"><input checked type="checkbox" name="roleIds[]" id="roleIds_' + data[i].id + '" class="roleIds" value="' + data[i].id + '" onclick="loadProduct(' + data[i].id + ')"> '+ data[i].name +'</label></div>';
					} else {
						roleHtml += '<div style="margin-top:10px; font-weight:bold;"><label onclick="loadProduct(' + data[i].id + ');"><input type="checkbox" name="roleIds[]" id="roleIds_' + data[i].id + '" class="roleIds" value="' + data[i].id + '" onclick="loadProduct(' + data[i].id + ')"> '+ data[i].name +'</label></div>';
					}
					
					
					roleHtml += '	<div style="margin-bottom:10px; display:none; padding-left:20px;" id="roleProduct_' + data[i].id + '">';
					if (exists == 1 && roleIsAllProduct == 1) {//如果角色存在并且角色的产品不限
						roleHtml += '		<div style="width:200px; float:left;"><label><input type="checkbox" name="roleIsAllProduct_' + data[i].id + '" class="roleIsAllProduct" value="' + data[i].id + '" id="roleIsAllProduct_' + data[i].id + '" onclick="loadProductDetail(' + data[i].id + ');" checked /> <span style="color:#f00">不限产品</span></label></div>';
					} else {
						roleHtml += '		<div style="width:200px; float:left;"><label><input type="checkbox" name="roleIsAllProduct_' + data[i].id + '" class="roleIsAllProduct" value="' + data[i].id + '" id="roleIsAllProduct_' + data[i].id + '" onclick="loadProductDetail(' + data[i].id + ');" /> <span style="color:#f00">不限产品</span></label></div>';
					}
					
					roleHtml += '		<div id="roleProductDetail_' + data[i].id + '" style="float:left;"></div>';
					
					roleHtml += '	</div>';
					roleHtml += '<div style="clear:both;"></div>';
					roleHtml += '</span>';
					$("#roleBlock").html($("#roleBlock").html() + roleHtml);
					if (exists == 1) {
						loadProduct(data[i].id);
					}
				}
			} else {
				$("#roleBlock").html('暂无角色');
			}
			
         } 
    });
}

function loadProduct(roleId)
{
	if ($('#roleIds_' + roleId).attr('checked') == true) {
		$('#roleProduct_' + roleId).css('display', '');
	} else {
		$('#roleProduct_' + roleId).css('display', 'none');
	}
	if ($('#roleIsAllProduct_' + roleId).attr('checked') == false) {
		loadProductDetail(roleId);
	}
}
function loadProductDetail(roleId)
{
	var type = null;
	$(".type").each(function(){
		if ($(this).attr('type') == 'radio' && $(this).attr('checked') == true) {
			type = this.value;
		}
	});
	var userRoleProductJsonData = <?php echo $this->userRoleProductJsonData;?>;

	var partnerProductJsonData = <?php echo $this->partnerProductJsonData;?>;
	var roleHtml = '';
	for(var n = 0; n <= partnerProductJsonData.length - 1; n++){
		if ((type == 2 && $("#partnerCode").val() == partnerProductJsonData[n].partnerCode) || type == 1) {
			var exists = 0;
			for (key in userRoleProductJsonData){
				if (userRoleProductJsonData[key].productCode == partnerProductJsonData[n].code && userRoleProductJsonData[key].roleId == roleId) {
					exists = 1;
					break;
				}
			}
			if (exists == 1) {
				roleHtml += '<div style="width:200px; float:left;"><label><input checked type="checkbox" name="userRoleProducts_' + roleId + '[]" class="userRoleProducts" value="' + partnerProductJsonData[n].code + '"> ' + partnerProductJsonData[n].name + '</label></div>';
			} else {
				roleHtml += '<div style="width:200px; float:left;"><label><input type="checkbox" name="userRoleProducts_' + roleId + '[]" class="userRoleProducts" value="' + partnerProductJsonData[n].code + '"> ' + partnerProductJsonData[n].name + '</label></div>';
			}
		}
	}
	roleHtml += '<div style="clear:both;"></div>';
	$("#roleProductDetail_" + roleId).html(roleHtml);
	if ($('#roleIsAllProduct_' + roleId).attr('checked') == false) {
		$('#roleProductDetail_' + roleId).css('display', '');
	} else {
		$('#roleProductDetail_' + roleId).css('display', 'none');
	}
}
</script>

<script language="javascript">
function checkPassword(str)
{
	if ($.trim($("#password").val()) != '' && $.trim($("#password").val()).length > 30) {
		return {'isRight':false, 'message':'密码长度不能大于30个字符。'};
	}
	if ($.trim($("#password").val()) != '' && $.trim($("#password").val()).length < 5) {
		return {'isRight':false, 'message':'密码长度不能小于5个字符。'};
	}
	
	return {'isRight':true, 'message':''};
}

function checkMobile(account)
{
    var url = '<?php echo $this->projectUrl(array('action'=>'get-user-is-exists-by-json', 'userId'=>$this->row['id'], 'account'=>'{account}'), NULL, false);?>'+'?rand/' + Math.random();
	url = url.replace('{account}', account);
	checkResult = {'isRight':true, 'message':''};

	if ($.trim($("#mobile").val()) != '' && $.trim($("#mobile").val()).search(/\d{11}/) == -1) {
		return {'isRight':false, 'message':'手机号码填写错误。'};
	}
	
	if (account != '') {
		$.ajax({
			type: "GET",
			url: url,
			dataType: "json",
			async: false,
			success: function(data){
				if (data == 1) {
					checkResult = {'isRight':false, 'message':'* 此手机号已经被他人使用过了，请换一个。'};
				}
			} 
		});
	}
	return checkResult;
}

function checkEmail(account)
{
    var url = '<?php echo $this->projectUrl(array('action'=>'get-user-is-exists-by-json', 'userId'=>$this->row['id'], 'account'=>'{account}'), NULL, false);?>'+'?rand/' + Math.random();
	url = url.replace('{account}', account);
	checkResult = {'isRight':true, 'message':''};

	if ($.trim($("#email").val()).search(/^[\w]+[\w\.-]*@[\w]+[\w-]*(\.[\w]+[\w-]*)+$/) == -1 && $.trim($("#email").val()) != '') {
		return {'isRight':false, 'message':'E-Mail地址填写错误。'};
	}
	
	if (account != '') {
		$.ajax({
			type: "GET",
			url: url,
			dataType: "json",
			async: false,
			success: function(data){
				if (data == 1) {
					checkResult = {'isRight':false, 'message':'* 此E-Mail已经被他人使用过了，请换一个。'};
				}
			} 
		});
	}
	return checkResult;
}

$("#editForm").submit(function(){
	var error = false;
	
	if ($('input[name=roleIds[]]:checked').size() == 0 ) {
		alert('你至少选择一个角色。');
		return false;
	}
	$(".roleIds").each(function(){
		if (this.checked == true) {
			if ($(this).parent().parent().parent().find('input[name=roleIsAllProduct_' + this.value + ']:checked').size() == 0 && $(this).parent().parent().parent().find('input[name=userRoleProducts_' + this.value + '[]]:checked').size() == 0) {
				alert('在选择角色时，必须选择一个角色产品。');
				error = true;
			}
		}
	});
	if (error) {
		return false;
	}
	return true;
});
</script>
</body>
</html>